#!/usr/bin/python3

import sys
from gi.repository import GLib

import dbus
import dbus.mainloop.glib

def print_usage():
	print "Usage: test-ss-control-cb <password>"
	sys.exit(1);

def property_changed(property, value):
	print("CallBarring property %s changed to %s" % (property, value))

def print_properties(cb):
	properties = cb.GetProperties()

	for p in properties:
		print("property %s, value: %s" % (p, properties[p]))

if __name__ == "__main__":
	if (len(sys.argv) != 2):
			print_usage()

	password = sys.argv[1]

	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

	bus = dbus.SystemBus()

	manager = dbus.Interface(bus.get_object('org.ofono', '/'),
							'org.ofono.Manager')

	modems = manager.GetModems()

	cb = dbus.Interface(bus.get_object('org.ofono', modems[0][0]),
                                                        'org.ofono.CallBarring')

	cb.connect_to_signal("PropertyChanged", property_changed)

	ss = dbus.Interface(bus.get_object('org.ofono', modems[0][0]),
							'org.ofono.SupplementaryServices')

	print_properties(cb)

	print("Trying invalid SS request for CB")
	try:
		print(ss.Initiate("*33#456666"))
	except dbus.DBusException as e:
		print("Failed with %s - Good" % e)

	print("Trying invalid SS request for CB")
	try:
		print(ss.Initiate("*33*ABC#"))
	except dbus.DBusException as e:
		print("Failed with %s - Good" % e)

	print("Trying invalid SS request for CB")
	try:
		print(ss.Initiate("*33**ABC#"))
	except dbus.DBusException as e:
		print("Failed with %s - Good" % e)

	print("Trying invalid SS request for CB")
	try:
		print(ss.Initiate("*33***12#"))
	except dbus.DBusException as e:
		print("Failed with %s - Good" % e)

	print("Query Outgoing All")
	print(ss.Initiate("*#33#"))

	print("Query Outgoing International")
	print(ss.Initiate("*#331#"))

	print("Query Outgoing except home country")
	print(ss.Initiate("*#332#"))

	print("Query Incoming All")
	print(ss.Initiate("*#35#"))

	print("Query Incoming while Roaming")
	print(ss.Initiate("*#351#"))

	print("Query All Outgoing")
	print(ss.Initiate("*#333#"))

	print("Query All Incoming")
	print(ss.Initiate("*#353#"))

	print("Query All")
	print(ss.Initiate("*#330#"))

	ss_string = "*33*" + password + "*11#"

	print("Enable Barring for Outgoing International calls for Voice")
	print(ss.Initiate(ss_string))

	print_properties(cb)

	ss_string = "#330*" + password + "#"

	print("Disable All Barrings")
	print(ss.Initiate(ss_string))

	sys.exit(1);

	mainloop = GLib.MainLoop()
	mainloop.run()

